package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;

import model.Duty;
import model.LeaveType;
import dao.LeaveTypeDAO;

public class LeaveTypeDAOImpl implements LeaveTypeDAO {

	private DBUtil dbu = new DBUtil();
	private Connection conn = null;
	private PreparedStatement pstat = null;
	private ResultSet rs = null;
	public List<LeaveType> getAll() {
		List <LeaveType> list=new ArrayList<LeaveType>();
		try {
			conn = dbu.getConnection();
			String sql="select * from LeaveType";
			pstat=conn.prepareStatement(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				LeaveType ltype = new LeaveType();
				ltype.setId(rs.getInt("ID"));
				ltype.setTye(rs.getString("TYPE"));
				list.add(ltype);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return list;
	}

	public LeaveType getById(int id) {
		conn= dbu.getConnection();
		LeaveType ltype = new LeaveType();
		try {
			pstat=conn.prepareStatement("select * from LeaveType where id="+id);
			rs=	pstat.executeQuery();
			while(rs.next()){
				ltype.setId(rs.getInt("ID"));
				ltype.setTye(rs.getString("TYPE"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		return ltype;
	}

	public LeaveType getByName(String name) {
		conn= dbu.getConnection();
		LeaveType ltype = new LeaveType();
		try {
			pstat=conn.prepareStatement("select * from LeaveType where name='"+name+"'");
			rs=	pstat.executeQuery();
			while(rs.next()){
				ltype.setId(rs.getInt("ID"));
				ltype.setTye(rs.getString("TYPE"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		return ltype;
	}

}
